/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package models;

import Utils.CursInfoValutar;
import Utils.DBConnection;
import Utils.Settings;
import Utils.Static;
import Utils.Utils;
import com.mysql.jdbc.Util;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;

/**
 *
 * @author user
 */
public class ContentModel {

    DBConnection db = new DBConnection(Settings.getDb(), Settings.getDbLogin(), Settings.getDbPassword());
    String message;

    public ArrayList GetPages() {
        try {
            String querry = "SELECT * FROM content";

            ResultSet rs = db.ExecuteQuery(querry);
            ArrayList Rows = Utils.resultSetToArrayList(rs);
            return Rows;
        } catch (Exception ex) {
            return null;
        } finally {
            //  db.close();
        }
    }

    public HashMap GetPage(int contentID) {
        try {


            String querry = "SELECT * FROM content"
                    + " WHERE ContentID=\"" + contentID + "\" ";

            ResultSet rs = db.ExecuteQuery(querry);
            ArrayList Rows = Utils.resultSetToArrayList(rs);
            return (HashMap) Rows.get(0);
        } catch (Exception ex) {
            return null;
        } finally {
            //  db.close();
        }
    }

    public String GetContent(String pageName) {
        try {

            String querry = "SELECT * FROM content"
                    + " WHERE Page=\"" + pageName + "\" ";

            ResultSet rs = db.ExecuteQuery(querry);
            ArrayList Rows = Utils.resultSetToArrayList(rs);
            return ((HashMap) Rows.get(0)).get("Content").toString();
        } catch (Exception ex) {
            return null;
        } finally {
            //  db.close();
        }
    }

    public String GetPicturePath(String pageName) {
        try {
            String querry = "SELECT * FROM content"
                    + " WHERE Page=\"" + pageName + "\" ";

            ResultSet rs = db.ExecuteQuery(querry);
            ArrayList Rows = Utils.resultSetToArrayList(rs);
            return ((HashMap) Rows.get(0)).get("PicturePath").toString();
        } catch (Exception ex) {
            return null;
        } finally {
            //  db.close();
        }
    }

    public int UpdateContent(int contentID, String content) {
        try {

            String query = "UPDATE content"
                    + " SET Content = \'" + content + "\' "
                    + "WHERE ContentID=" + contentID + " ";
            int rows = db.ExecuteUpdate(query);

            if (rows > 0) {
                message = "Pagina a fost actualizata cu succes!";
                return 1;
            } else {
                message = "Pagina nu a fost actualizata!";
                return -1;
            }

        } catch (Exception ex) {
            message = "Eroare cod 001";
            return -1;
        } finally {
            //     db.close();
        }
    }

    public String GetMessage() {
        return message;
    }
}
